<%@page import="wt.epm.EPMDocument"%>
<%@page import="ext.acae2.util.VersionUtil"%>
<%@page import="wt.vc.VersionControlHelper"%>
<%@page import="wt.vc.config.LatestConfigSpec"%>
<%@page import="wt.fc.WTObject"%>
<%@page import="wt.part.WTPart"%>
<%@page import="wt.doc.WTDocument"%>
<%@page import="ext.acae2.baseline.web.BaselineUtil"%>
<%@page import="java.util.List"%>
<%@page import="wt.vc.baseline.ManagedBaseline"%>
<%@page import="wt.fc.ReferenceFactory"%>
<%@taglib prefix="wctags" tagdir="/WEB-INF/tags" %>
<%@taglib uri="http://www.ptc.com/windchill/taglib/fmt" prefix="fmt" %>
<%@page import="ext.acae2.baseline.resource.BaselineActionResource" %>

<fmt:setBundle basename="ext.acae2.baseline.resource.BaselineActionResource"/>
<fmt:message var="baseline" key="<%=BaselineActionResource.BASELINE%>" />
<fmt:message var="name" key="<%=BaselineActionResource.NAME%>" />
<fmt:message var="number" key="<%=BaselineActionResource.NUMBER%>" />
<fmt:message var="dataOriginalState" key="<%=BaselineActionResource.DATA_ORIGINAL_STATE%>" />
<fmt:message var="dataLatestState" key="<%=BaselineActionResource.DATA_LATEST_STATE%>" />
<fmt:message var="lastUpdate" key="<%=BaselineActionResource.LAST_UPDATE%>" />
<fmt:message var="version" key="<%=BaselineActionResource.VERSION%>" />
<fmt:message var="state" key="<%=BaselineActionResource.STATE%>" />
<fmt:message var="downloadReport" key="<%=BaselineActionResource.DOWNLOAD_REPORT%>" />

<%
String oid = request.getParameter("oid");
ReferenceFactory refFactory = new ReferenceFactory();
ManagedBaseline baseline = (ManagedBaseline) refFactory.getReference(oid).getObject();
List<Object> baselineDataList = BaselineUtil.getBaselineObjects(baseline);

String serverName = request.getServerName();
String contextPath = request.getContextPath();
String scheme = request.getScheme();
String fullPath = scheme+"://"+serverName+contextPath;

System.out.println("baselineDataList size: " + baselineDataList.size());
%>

<input id="oid" type="hidden" value="<%=oid%>" />

<link rel="stylesheet" href="netmarkets/jsp/ocmp/css/comparisonReport.css" type="text/css" />

<table width='80%' align="center">
    <tr>
        <td><font style="font-weight: bold;">${baseline}: </font></td>
        <td><%=baseline.getNumber()%></td>
    </tr>
</table>

<br/>

<table width='80%' cellpadding='0' cellspacing='0' style='margin-left:10%;border:1px solid black;'>
    <tr>
        <td rowspan='2' class="mainHeader"><font style="font-weight: bold;">${name}</font></td>
        <td rowspan='2' class="mainHeader"><font style="font-weight: bold;">${number}</font></td>
        <td colspan='2' class="mainHeader"><font style="font-weight: bold;">${dataOriginalState}</font></td>
        <td colspan='2' class="mainHeader"><font style="font-weight: bold;">${dataLatestState}</font></td>
        <td rowspan='2' class="mainHeader"><font style="font-weight: bold;">${lastUpdate}</font></td>
    </tr>
    <tr>
        <td class="mainHeader"><font style="font-weight: bold;">${version}</font></td>
        <td class="mainHeader"><font style="font-weight: bold;">${state}</font></td>
        <td class="mainHeader"><font style="font-weight: bold;">${iteration}</font></td>
        <td class="mainHeader"><font style="font-weight: bold;">${state}</font></td>
    </tr>
    <% for (Object data : baselineDataList) { 
        if (data instanceof WTPart) { 
            WTPart part = (WTPart) data;
            WTPart latestPart = (WTPart) VersionUtil.getLatestObject(part);
    %>
            <tr>
                <td class="background1">
                    <img src="http://pdm.acae.com/Windchill/netmarkets/images/part.gif">
                    <%=part.getName()%>
                </td>
                <td class="background1"><%=part.getNumber()%></td>
                <td class="background1"><%=part.getVersionIdentifier().getValue()%>.<%=part.getIterationIdentifier().getValue()%></td>
                <td class="background1"><%=part.getLifeCycleState().getDisplay()%></td>
                <td class="background1"><%=latestPart.getVersionIdentifier().getValue()%>.<%=latestPart.getIterationIdentifier().getValue()%></td>
                <td class="background1"><%=latestPart.getLifeCycleState().getDisplay()%></td>
                <td class="background1"><%=latestPart.getModifyTimestamp().toString()%></td>
            </tr>
        <% 
        }
        else if (data instanceof WTDocument) {
            WTDocument doc = (WTDocument) data;
            WTDocument latestDoc = (WTDocument) VersionUtil.getLatestObject(doc);
        %>
            <tr>
                <td class="background1">
                    <img src="http://pdm.acae.com/Windchill/netmarkets/images/doc_document.gif">
                    <%=doc.getName()%>
                </td>
                <td class="background1"><%=doc.getNumber()%></td>
                <td class="background1"><%=doc.getVersionIdentifier().getValue()%>.<%=doc.getIterationIdentifier().getValue()%></td>
                <td class="background1"><%=doc.getLifeCycleState().getDisplay()%></td>
                <td class="background1"><%=latestDoc.getVersionIdentifier().getValue()%>.<%=latestDoc.getIterationIdentifier().getValue()%></td>
                <td class="background1"><%=latestDoc.getLifeCycleState().getDisplay()%></td>
                <td class="background1"><%=latestDoc.getModifyTimestamp().toString()%></td>
            </tr>
        <%
        }
        else if (data instanceof EPMDocument) {
            EPMDocument epm = (EPMDocument) data;
            EPMDocument latestEPM = (EPMDocument) VersionUtil.getLatestObject(epm);
        %>
            <tr>
                <td class="background1">
                    <img src="wt/clients/images/ug/epm_ug_part.gif">
                    <%=epm.getName()%>
                </td>
                <td class="background1"><%=epm.getNumber()%></td>
                <td class="background1"><%=epm.getVersionIdentifier().getValue()%>.<%=epm.getIterationIdentifier().getValue()%></td>
                <td class="background1"><%=epm.getLifeCycleState().getDisplay()%></td>
                <td class="background1"><%=latestEPM.getVersionIdentifier().getValue()%>.<%=latestEPM.getIterationIdentifier().getValue()%></td>
                <td class="background1"><%=latestEPM.getLifeCycleState().getDisplay()%></td>
                <td class="background1"><%=latestEPM.getModifyTimestamp().toString()%></td>
            </tr>
        <% } %>
    <% } %>
</table>

<table width='80%' align="center">
    <tr>
        <td>
            <input type="button" value="${downloadReport}" onclick="download()"/>
        </td>
    </tr>
</table>

<script>
function download() {
    window.open("<%=fullPath%>/netmarkets/jsp/ext/acae2/baseline/downloadCompareBaselineLatestData.jsp?oid=" 
            + document.getElementById("oid").value);
}
</script>